package com.neuedu.mvc.service;

import com.neuedu.mvc.dao.DepartmentDao;
import com.neuedu.mvc.entity.Department;

import javax.servlet.http.HttpServletRequest;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务逻辑层
 */
public class DepartmentService {

    DepartmentDao departmentDao = new DepartmentDao();


    /**
     * 根据条件查询列表
     * @param request
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public List<Department> list(HttpServletRequest request) throws SQLException, ClassNotFoundException {

        String name = request.getParameter("name");

        String sql = "select * from department ";
        Object[] params =  null;
        if(name!=null && name.trim().length()>0){
            sql += " where name like ? ";
            params = new Object[]{"%"+name+"%"};
        }


        List<Department> departments = departmentDao.selectList(sql,params);

        return departments ;
    }


    /**
     * 带分页的查询
     * @param request
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public Map listPage(HttpServletRequest request) throws SQLException, ClassNotFoundException {

        String name = request.getParameter("name");

        String sql = "select * from department ";
        Object[] params =  null;
        if(name!=null && name.trim().length()>0){
            sql += " where name like ? ";
            params = new Object[]{"%"+name+"%"};
        }

        int current = 1;
        int size = 10;
        try{
            if(request.getParameter("current") != null) {
                current = Integer.parseInt(request.getParameter("current"));
            }
            if(request.getParameter("size") != null) {
                size = Integer.parseInt(request.getParameter("size"));
            }
        }finally{}

        List<Department> departments = departmentDao.selectPage(sql ,current,size,params);
        long total = departmentDao.count(sql,params);


        Map result = new HashMap();

        result.put("current",current);
        result.put("size",size);
        result.put("total",total);
        result.put("data",departments);


        return result ;
    }


    /**
     * 根据输入的信息添加一条部门
     *  `name` varchar(100) DEFAULT NULL COMMENT '名称',
     *   `address` varchar(200) DEFAULT NULL COMMENT '办公地址',
     *   `leader` varchar(100) DEFAULT NULL COMMENT '负责人',
     * @param request
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public boolean save(HttpServletRequest request) throws SQLException, ClassNotFoundException {

        String name = request.getParameter("name");
        String address = request.getParameter("address");
        String leader = request.getParameter("leader");

        String sql = "insert into department (name, address,leader) values(?,?,?) ";

        int count = departmentDao.insert(sql,name,address,leader);

        return count >0;


    }


    /**
     * 根据主键查询信息
     * @param request
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public Department queryById(HttpServletRequest request) throws SQLException, ClassNotFoundException {
        String id = request.getParameter("id");

        return departmentDao.selectOne(Integer.parseInt(id));
    }


    /**
     * 更具主键更新
     * @param request
     * @return
     */
    public boolean update(HttpServletRequest request) throws SQLException, ClassNotFoundException {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String address = request.getParameter("address");
        String leader = request.getParameter("leader");

        String sql = "UPDATE   department SET  `name` = ?, `address` = ?  ,  `leader` = ? WHERE `id` =  ?  ";

        int count = departmentDao.update(sql,name,address,leader,id);


        return count >0;
    }


    /**
     *  根据主键删除一条数据
     * @param request
     * @return
     * @throws SQLException
     * @throws ClassNotFoundException
     */
    public boolean removeById(HttpServletRequest request ) throws SQLException, ClassNotFoundException {

        String id = request.getParameter("id");
        return departmentDao.deleteById(Integer.parseInt(id)) > 0;

    }
}
